package com.handydata.antifraud.rule.funcs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.handydata.antifraud.common.constant.AlarmInfoCode;
import com.handydata.antifraud.common.constant.DictInfoCode;
import com.handydata.antifraud.to.RiskInputTO;
import com.handydata.antifraud.to.RiskMiddleTO;
import com.handydata.common.util.RedisUtil;

/**
 * 计算手机安装多种类型app的数量
 */
@Slf4j
@Component
public class CalcAppNums extends CalculateAdapter {
    @Autowired
    private RedisUtil redisUtil;
    @SuppressWarnings("unchecked")
	@Override
    public void doCalc(RiskInputTO riskInputTO, RiskMiddleTO riskMiddleTO) {
        List<String> appList = riskInputTO.getAppList();

        if(appList==null||appList.isEmpty()){
            return ;
        }
        List<String> arr=new ArrayList<String>();
        for(String key:appList){
            arr.add(AlarmInfoCode.REDIS_GENERAL_DICT.getCode()+"_"+DictInfoCode.DICT_APP_CAT.getCode()+"_"+key);
        }
        List<String> type1s=(List<String>)(List)redisUtil.multiGet(arr);

        arr=new ArrayList<String>();
        for(String key:appList){
            arr.add(AlarmInfoCode.REDIS_GENERAL_DICT.getCode()+"_"+DictInfoCode.DICT_APP_TYPE.getCode()+"_"+key);
        }
        List<String> type2s=(List<String>)(List)redisUtil.multiGet(arr);

        log.info("start doCalc applist {} type1s{} type2{}",appList,type1s,type2s);

        int appLoanCount = count(appList, str -> str.contains("贷"));
        int appGrimaceCount = count(appList, str -> str.contains("iGrimace"));
        int appIgvxCount = count(appList, str -> str.contains("igvx"));
        int appBocDubCount = countType(type1s, "博彩类_赌博性质");
        int appYouxDubCount = countType(type1s, "游戏类_可能涉及赌博");
        int appLoanSmallLoanCount = countType(type1s, "贷款类_小额贷款");
        int appLoanCreditCardCount = countType(type1s,"贷款类_信用卡代偿");
        int appLoanOtherCount = countType(type1s, "贷款类_其它");
        int appBankCount = countType(type2s, "银行");
        int appDebitLoanCount = countType(type2s, "借贷");
        int appWifiCount = countType(type2s, "wifi");
        int appHcpCount = countType(type2s, "火车票");
        int appPayCount = countType(type2s, "支付");
        int appJzCount = countType(type2s, "记账");
        int appMrlzCount = countType(type2s, "美容靓装");
        int appYtjCount = count(appList, str -> str.contains("一体机"));
        int appCount = count(appList, str -> true);
        int appStartTestCount = count(appList, str -> str.startsWith("test"));
        int calAAppR020Count = count(appList, str -> match(str, "支付宝|手机淘宝|微信|QQ|银行|腾讯视频|百度|拼多多|京东|头条"));

        riskMiddleTO.setAppLoanCount(appLoanCount);
        riskMiddleTO.setAppGrimaceCount(appGrimaceCount);
        riskMiddleTO.setAppIgvxCount(appIgvxCount);
        riskMiddleTO.setAppBocDubCount(appBocDubCount);
        riskMiddleTO.setAppYouxDubCount(appYouxDubCount);
        riskMiddleTO.setAppLoanSmallLoanCount(appLoanSmallLoanCount);
        riskMiddleTO.setAppLoanCreditCardCount(appLoanCreditCardCount);
        riskMiddleTO.setAppLoanOtherCount(appLoanOtherCount);
        riskMiddleTO.setAppLoanOtherCount(appLoanOtherCount);
        riskMiddleTO.setAppBankCount(appBankCount);
        riskMiddleTO.setAppDebitLoanCount(appDebitLoanCount);
        riskMiddleTO.setAppWifiCount(appWifiCount);
        riskMiddleTO.setAppHcpCount(appHcpCount);
        riskMiddleTO.setAppPayCount(appPayCount);
        riskMiddleTO.setAppJzCount(appJzCount);
        riskMiddleTO.setAppMrlzCount(appMrlzCount);
        riskMiddleTO.setAppYtjCount(appYtjCount);
        riskMiddleTO.setAppCount(appCount);
        riskMiddleTO.setAppStartTestCount(appStartTestCount);
        riskMiddleTO.setCalAAppR020Count(calAAppR020Count);
    }


    private int count(List<String> appList, Function<String, Boolean> func) {
        int count = 0;
        if(CollectionUtils.isEmpty(appList)) {
            return count;
        }
        for(String app : appList) {
            boolean bl = func.apply(app);
            if(bl) {
                count++;
            }
        }

        return count;
    }

    private int countType(List<String> types, String searchType){
        int count=0;
        int index=0;
        for(String type : types) {
            if(type!=null&&type.indexOf(searchType)!=-1) {

                count++;
                //System.out.println(index);
            }
            index++;
        }
        return count;
    }

    private boolean match(String app, String apps) {
        String[] appList = apps.split("\\|");
        return Arrays.asList(appList).stream().filter(a -> a.equals(app)).count() > 0;
    }
}
